package com.gzbugu.bugusys.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.gzbugu.bugusys.bean.PageModel;
import com.gzbugu.bugusys.dao.IBaseDAO;
import com.gzbugu.bugusys.dao.IHQLQuery;
import com.gzbugu.bugusys.service.IFindTravelService;
import com.gzbugu.bugusys.util.imp.HQLQuery;

/**
 * @author Bokchoy 查找差旅信息
 */
@Service
public class FindTravelService implements IFindTravelService {

	@Resource(name = "hqlQuery")
	private IHQLQuery hqlQuery;
	private PageModel pageModel;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.gzbugu.bugusys.service.IFindTravelService#findTravelByMonth(java.
	 * lang.Integer, java.lang.Integer)
	 */
	public List findTravelByMonth(Integer year, Integer month) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month - 1, 1, 0, 0);
		Date start = calendar.getTime();// 开始日期为指定月份第一天
		calendar.set(Calendar.DATE, calendar.getMaximum(Calendar.DATE));
		Date end = calendar.getTime();// 结束日期为指定月份的最后一天
		String query = "from Travel as model where model.start >=:start and model.start <=:end";
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("start", start);
		paramsMap.put("end", end);
		return hqlQuery.find(query, paramsMap, this.getPageModel());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.gzbugu.bugusys.service.IFindTravelService#findTravelByStaffId(java
	 * .lang.String)
	 */
	public List findTravelByStaffId(String staffId) {
		String query = "from Travel as model where model.staff.staffId ='"
				+ staffId + "'";
		return hqlQuery.find(query, pageModel);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.gzbugu.bugusys.service.IFindTravelService#findTravelByDateExtent(
	 * java.util.Date, java.util.Date)
	 */
	public List findTravelByDateExtent(Date travelStart, Date travelEnd) {
		String query = "from Travel as model where model.date >='"
				+ travelStart + "' and model.end <='" + travelEnd + "'";
		return hqlQuery.find(query, this.getPageModel());
	}

	public void setPageModel(PageModel pageModel) {
		this.pageModel = pageModel;
	}

	public PageModel getPageModel() {
		if (this.pageModel == null)
			return new PageModel();
		return pageModel;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.gzbugu.bugusys.service.IFindTravelService#findTravelByMonthAndId(
	 * java.lang.String, java.lang.String, java.lang.String)
	 */
	public List findTravelByMonthAndId(String staffId, Integer year,
			Integer month) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month - 1, 1, 0, 0);
		Date start = calendar.getTime();// 开始日期为指定月份第一天
		calendar.set(Calendar.DATE, calendar.getMaximum(Calendar.DATE));
		Date end = calendar.getTime();// 结束日期为指定月份的最后一天
		String query = "from Travel as model where model.staff.staffId =:staffId and model.start >=:start and model.start <=:end";
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("staffId", staffId);
		paramsMap.put("start", start);
		paramsMap.put("end", end);
		return hqlQuery.find(query, paramsMap, this.getPageModel());
	}
}
